﻿(*                  FnSharp              

   Base class library extensions and frameworks aimed 
   specifically at improving F# developers lives!

   Written by: Huw Simpson
   License: Microsoft Public License (Ms-PL) 
   Available at: http://fnsharp.codeplex.com

*)
namespace FnSharp.Bcl.Core

    open System

    [<RequireQualifiedAccess>]
    [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
    module Disposable =
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        [<CompiledName("Dispose")>]
        let dispose (disposable:#IDisposable) =
            try disposable.Dispose () with _ -> () 

        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources on multiple disposable instances.
        [<CompiledName("DisposeAll")>]
        let disposeAll (disposables:#IDisposable seq) =
            disposables |> Seq.iter (fun disposable -> dispose disposable)

        /// Initialize a new disposable instance, which when disposed calls a function.
        [<CompiledName("Init")>]
        let init f =
            { new IDisposable with
                member self.Dispose () = f () }

        /// Create a new disposable instance.
        [<CompiledName("Empty")>]
        let empty =
            { new IDisposable with
                member self.Dispose () = () }

        /// Create a new disposable instance, which wraps multiple disposable instances.
        [<CompiledName("OfDisposables")>]
        let ofDisposables disposables =
            { new IDisposable with
                member self.Dispose () = disposeAll disposables }

    [<AutoOpen>]
    module DisposablePervasives =
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        [<CompiledName("Dispose")>]
        let dispose (disposable:#IDisposable) =
            try disposable.Dispose () with _ -> () 

        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources on multiple disposable instances.
        [<CompiledName("DisposeAll")>]
        let disposeAll (disposables:#IDisposable seq) =
            disposables |> Seq.iter (fun disposable -> dispose disposable)
